/**
 * 抓马界面
 * */
var ZhuaMaLayer = cc.Layer.extend({
    ctor: function (data,room) {
        this._super();
        loadFile(this, majiang.res.ZhuaMaLayer_json);
        this.rootNode.x = cc.winSize.width / 2;
        this.rootNode.y = cc.winSize.height / 2;
        for (var i = 0; i < 8; i++) {
            safeHide(this['_pai' + i]);
        }

        var A = 0.1, B = 0.2;
        var uid = data['zhuama_uid'];
        var zhuama_uid_arr = data['zhuama_uid_arr'] ||[];
        var zhongma_arr = data['zhongma_arr'] ||[];
        var playerInfo = gameData.getPlayerInfoByUid(uid);
        var nickname = playerInfo ? decodeURIComponent(playerInfo.nickname) : '';
        this['_des'].setString('玩家【' + ellipsisStr(nickname, 7) + '】正在抓码...');
        var show_ops_arr = [];
        var pai_arr = data['ma_arr'];
        var  posArray = [];

        var temp = [];

        for (var j = 0; j < pai_arr.length; j++) {
            var pai = this['_pai' + j];
            if (!pai) {
                continue;
            }
            pai.setVisible(true);
            var paiName = 'p2l' + pai_arr[j] + '.png';
            setSpriteFrameByName(pai, paiName, 'pai');
            pai.setOpacity(0);
            pai.runAction(cc.sequence(cc.delayTime(j * A), cc.fadeIn(B)));
            show_ops_arr.push(pai);
            for(var k = 0 ; k < zhongma_arr.length ;k++){
                if(pai_arr[j] == zhongma_arr[k] && temp.indexOf(j) < 0){
                    temp.push(j);
                    var beginPos = pai.getParent().convertToWorldSpace(pai.getPosition());
                    var zhuamadata = {};
                    zhuamadata.beginPos = beginPos;
                    zhuamadata.uid = uid;
                    posArray.push(zhuamadata);
                }
            }

        }
        var offset_x = 90;
        var start_x = 640;
        if (show_ops_arr.length > 1) {
            start_x = start_x - (show_ops_arr.length - 1) * offset_x / 2;
        }
        j = 0;
        for (; j < show_ops_arr.length; j++) {
            var show_ops = show_ops_arr[j];
            if (cc.sys.isObjectValid(show_ops)) {
                show_ops.x = start_x;
                show_ops.setVisible(true);
                start_x += offset_x;
            }
        }
        var that = this;
        var time = 1 + A * show_ops_arr.length ;
        this.scheduleOnce(function () {
            room.zhuamaAni(posArray,time);
        }, 0.8);
        this.scheduleOnce(function () {
            if (cc.sys.isObjectValid(that)) {
                that.removeFromParent();
                network.start();
            }
        }, time);
    },

    onExit: function () {
        cc.Layer.prototype.onExit.call(this);
    }
});